package com.pinyougou.manager.controller;

import java.util.List;

import com.alibaba.fastjson.JSON;
//import com.pinyougou.page.service.ItemPageService;
import com.pinyougou.pojo.TbItem;
import com.pinyougou.pojogroup.GoodsAndDesc;
import com.pinyougou.sellergoods.service.ItemService;
import entity.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.TbGoods;
import com.pinyougou.sellergoods.service.GoodsService;

import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RestController;


import javax.jms.*;



/**
 * controller
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Reference
    private GoodsService goodsService;

//	@Reference(timeout = 10000)
//	private ItemSearchService itemSearchService;

    @Autowired
    private Destination queueSolrDestination;//用于发送solr导入的消息(点对点)

    @Autowired
    private Destination queueSolrDeleteDestination;//用户在索引库中删除记录(点对点)

    @Autowired
    private Destination topicPageDestination;//用于生产商品详细页的消息目标(发布订阅)

    @Autowired
    private Destination topicPageDeleteDestination;//用于删除静态网页的消息

    @Autowired
    private JmsTemplate jmsTemplate;

    @Reference
    private ItemService itemService;

//    @Reference(timeout = 40000)
//    private ItemPageService itemPageService;

    /**
     * 返回全部列表
     *
     * @return
     */
    @RequestMapping("/findAll")
    public List<TbGoods> findAll() {
        return goodsService.findAll();
    }


//	/**
//	 * 返回全部列表
//	 * @return
//	 */
//	@RequestMapping("/findPage")
//	public PageInfo<TbGoods>  findPage(int page,int rows){
//		return goodsService.findPage(page, rows);
//	}

    /**
     * 增加
     *
     * @param goodsAndDesc
     * @return
     */
    @RequestMapping("/add")
    public ResultInfo add(@RequestBody GoodsAndDesc goodsAndDesc) {
        try {
            goodsService.add(goodsAndDesc);
            return new ResultInfo(true, "增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultInfo(false, "增加失败");
        }
    }

    /**
     * 修改
     *
     * @param goodsAndDesc
     * @return
     */
    @RequestMapping("/update")
    public ResultInfo update(@RequestBody GoodsAndDesc goodsAndDesc) {
        try {
            goodsService.update(goodsAndDesc);
            return new ResultInfo(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultInfo(false, "修改失败");
        }
    }

    /**
     * 获取实体
     *
     * @param id
     * @return
     */
    @RequestMapping("/findOne")
    public GoodsAndDesc findOne(Long id) {
        return goodsService.findOne(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    public ResultInfo delete(final Long[] ids) {
        try {
            goodsService.delete(ids);//删除数据库
            //itemSearchService.deleteByGoodsIds(Arrays.asList(ids));//删除solr库

            //删除solr索引库
            jmsTemplate.send(queueSolrDeleteDestination, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    return session.createObjectMessage(ids);
                }
            });

            //删除页面
            jmsTemplate.send(topicPageDeleteDestination, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    return session.createObjectMessage(ids);
                }
            });


            return new ResultInfo(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultInfo(false, "删除失败");
        }
    }

    /**
     * 查询+分页
     *
     * @param goods
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/search")
    public PageInfo<TbGoods> search(@RequestBody TbGoods goods, int page, int rows) {
        return goodsService.findPage(goods, page, rows);
    }

    /**
     * 批量修改商品审核状态
     *
     * @param ids
     * @param status
     * @return
     */
    @RequestMapping("/updateStatus")
    public ResultInfo updateStatus(Long[] ids, String status) {

        try {
            goodsService.updateStatus(ids, status);

            //根据SPU id查询SKU 列表(状态为1)
            if (status.equals("1")) {//审核通过
                //***导入到索引库
                //得到需要导入的SKU列表
                List<TbItem> itemList = goodsService.findItemListByGoodsIdandStatus(ids, status);
                //导入到solr
                //itemSearchService.importList(itemList);

                //调用搜索接口实现数据批量导入
                if (itemList.size() > 0) {
                    final String jsonString = JSON.toJSONString(itemList);
                    jmsTemplate.send(queueSolrDestination, new MessageCreator() {
                        @Override
                        public Message createMessage(Session session) throws JMSException {
                            return session.createTextMessage(jsonString);
                        }
                    });
                }

                //***静态页生成(运营商后台调用页面生成服务生成**商品详细页**)
                for (final Long goodsId : ids) {
//                    itemPageService.genItemHtml(goodsId);
                    jmsTemplate.send(topicPageDestination, new MessageCreator() {
                        @Override
                        public Message createMessage(Session session) throws JMSException {

                            return session.createTextMessage(goodsId+"");
                        }
                    });
                }

            }

            return new ResultInfo(true, "成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultInfo(false, "失败");
        }
    }

    /**
     * 生成静态页（测试）
     *
     * @param goodsId
     */
    @RequestMapping("/genHtml")
    public void genHtml(Long goodsId) {
//        itemPageService.genItemHtml(goodsId);

    }

}
